<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/static/jquery-1.11.0.min.js"></script>
</head>
<body>
<!--
 <form action="/UploadServlet" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="提交">
</form> 
-->

<form>
    <input type="file" name="file" id="file">
    <input type="submit" value="提交">
</form>


<script type="text/javascript">
/* document.querySelector('form').onsubmit = function(e){
	e.preventDefault();
	
	var formData = new FormData();
	formData.append("file", document.getElementById("file").files[0]);
	
	var xhr = new XMLHttpRequest();
	xhr.open('post', '/UploadServlet');
	xhr.onreadystatechange = function(){
		if(xhr.readyState === 4 && xhr.status === 200){
			alert('成功')
		}
	}
	xhr.send(formData);
} */

$('form').on('submit', function(e){
	e.preventDefault();
    var formData = new FormData();
    formData.append("file", document.getElementById("file").files[0]);
    
    $.ajax({
        url :'/UploadServlet',
        type:'post',
        data:formData,
        contentType:false, //不要头
        processData:false, // 不转换数据
        success:function(){
            alert("ok")
        }
    });
})
</script> 
</body>
</html>